from fastapi import APIRouter, Depends
from typing import  List, Any
from schemas.common import RETURN, ListRequest

import models.admin.system.Admin as AdminModel
from routers.admin.common import convert_filter_values
from core.Depend import  has_permission

from models.Base import StatusTypeEnum
from server.Base import BaseServer

router = APIRouter(prefix="/login")


# 查询登录日志
@router.post("/logs",summary="查询登录日志",response_model=RETURN[Any])
async def get_login_logs(request:ListRequest,user: AdminModel.redis_admin = Depends(has_permission("system_monitor_logs_list"))):
  conversion_map = {
      "status": StatusTypeEnum,
  }
  request.filter = await convert_filter_values(conversion_map,request.filter)
  data = await BaseServer(AdminModel.Admin_Login_Record).list(request)
  return RETURN.success("查询成功",data)

# 批量删除登录日志
@router.delete("/batch_delete",summary="批量删除登录日志",response_model=RETURN[Any])
async def delete_login_logs(ids: List[int],user: AdminModel.redis_admin = Depends(has_permission("system_monitor_logs_del_batch"))):
  await BaseServer(AdminModel.Admin_Login_Record).batch_delete(ids)
  return RETURN.success("删除成功")

# 清楚登录日志
@router.delete("/clear",summary="清除登录日志",response_model=RETURN[Any])
async def clear_login_logs(user: AdminModel.redis_admin = Depends(has_permission("system_monitor_logs_del_all"))):
  await BaseServer(AdminModel.Admin_Login_Record).clear()
  return RETURN.success("清空成功")